﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp.ChildWindows
{
    public partial class ChildWindow_Voyage : ChildWindow
    {
        ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();

        ObservableCollection<ServiceReference_HB.MainSeaRoutes> list_allSeaRoutes = null;//大航线.

        /// <summary>
        /// 服务航线ID。
        /// </summary>
        public int SCD_SLN_ID = 0;//

        /// <summary>
        /// 是否新增还是修改，为true表示是新增。
        /// </summary>
        public bool Is_Add_Modefy = false;
        /// <summary>
        /// 保存船期的ID。
        /// </summary>
        public string mSCD_ID = string.Empty;

        /// <summary>
        /// 当前登陆人员ID。
        /// </summary>
        public string mUser_ID = string.Empty;

        /// <summary>
        /// 记录当前选择的航次。
        /// </summary>
        public ServiceReference_HB.UM_Voyages mVoyages = null;

        /// <summary>
        /// 新增事件。
        /// </summary>
        public event MouseButtonEventHandler OnAdd_ModefyEvent = null;

        /// <summary>
        /// 修改事件.
        /// </summary>
        public event MouseButtonEventHandler OnModefyEvent = null;
            


        public ChildWindow_Voyage()
        {
            InitializeComponent();
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            progressbar.IsBusy = true;
            if (mUser_ID == "1")
                clientService.GetMainSeaRoutes_ByUserIdAsync(int.Parse(mUser_ID), "1");
            else
                clientService.GetMainSeaRoutes_ByUserIdAsync(int.Parse(mUser_ID), "0");
            clientService.GetMainSeaRoutes_ByUserIdCompleted -= new EventHandler<ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs>(clientService_GetMainSeaRoutes_ByUserIdCompleted);
            clientService.GetMainSeaRoutes_ByUserIdCompleted += new EventHandler<ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs>(clientService_GetMainSeaRoutes_ByUserIdCompleted);

        }
        System.Collections.ObjectModel.ObservableCollection<ServiceReference_HB.UM_Ref_Code> mList_Trade = null;
        void clientService_GetRefCodeCompleted(object sender, ServiceReference_HB.GetRefCodeCompletedEventArgs e)
        {
            mList_Trade = e.Result;
            comboxINTRADE.ItemsSource = mList_Trade;
            Inited();
            progressbar.IsBusy = false;
        }
        /// <summary>
        /// 修改数据时初始化。
        /// </summary>
        public void Inited()
        {
            if (mVoyages == null)
            {
                return;
            }
            if (SCD_SLN_ID != 0)
            {
                clientService.GetService_LinesBySLN_IDAsync(SCD_SLN_ID.ToString());//再获取服务航线信息。
                clientService.GetService_LinesBySLN_IDCompleted -= new EventHandler<ServiceReference_HB.GetService_LinesBySLN_IDCompletedEventArgs>(clientService_GetService_LinesBySLN_IDCompleted);
                clientService.GetService_LinesBySLN_IDCompleted += new EventHandler<ServiceReference_HB.GetService_LinesBySLN_IDCompletedEventArgs>(clientService_GetService_LinesBySLN_IDCompleted);
            }
            if (mVoyages.VOY_IEFG == "I")
            {
                rbtnImport.IsChecked = true;
                rbtnExport.IsChecked = false;
            }
            else
            {
                rbtnImport.IsChecked = false;
                rbtnExport.IsChecked = true;
            }

            txtImportVoyage.Text = mVoyages.VOY_VOYAGE;
            txtCountTen_Plan.Text = mVoyages.VOY_PTONNAGE == null ? "" : mVoyages.VOY_PTONNAGE.Value.ToString();
            txtCountTen.Text = mVoyages.VOY_LTONNAGE == null ? "" : mVoyages.VOY_LTONNAGE.Value.ToString();
            if (mList_Trade != null)
            {
                List<ServiceReference_HB.UM_Ref_Code> result_trade = (from table in mList_Trade
                                                                      where table.RV_LOW_VALUE == mVoyages.VOY_INTRADE
                                                                      select table).ToList();
                if (result_trade != null && result_trade.Count > 0)
                    comboxINTRADE.SelectedItem = result_trade[0];
            }


        }
        void clientService_GetService_LinesBySLN_IDCompleted(object sender, ServiceReference_HB.GetService_LinesBySLN_IDCompletedEventArgs e)
        {
            ServiceReference_HB.ST_SERVICE_LINES mService_Line = e.Result as ServiceReference_HB.ST_SERVICE_LINES;

            if (list_allSeaRoutes == null) return;
            //航线
            var result_SeaRout = (from table in list_allSeaRoutes
                                  where table.ID == mService_Line.SLN_SRT_ID
                                  select table).ToList();
            if (result_SeaRout != null && result_SeaRout.Count > 0)
            {
                comboxSeaRoutes.SelectedItem = result_SeaRout[0];
                //获取登陆人的名字
                int intID = 0;
                int.TryParse(mUser_ID, out intID);
                //绑定服务航线的下拉框
                if (intID == 1)
                    clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "1", result_SeaRout[0].ID);
                else
                    clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "0", result_SeaRout[0].ID);
            }
        }

        /// <summary>
        /// 航线名称完成事件。
        /// </summary>
        void clientService_GetMainSeaRoutes_ByUserIdCompleted(object sender, ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs e)
        {
            list_allSeaRoutes = e.Result;
            comboxSeaRoutes.ItemsSource = list_allSeaRoutes;
            //取内外贸标志 ：
            clientService.GetRefCodeAsync("rv_domain ='VOYAGE_INTRADE'", string.Empty);
            clientService.GetRefCodeCompleted -= new EventHandler<ServiceReference_HB.GetRefCodeCompletedEventArgs>(clientService_GetRefCodeCompleted);
            clientService.GetRefCodeCompleted += new EventHandler<ServiceReference_HB.GetRefCodeCompletedEventArgs>(clientService_GetRefCodeCompleted);
        }
        //保存
        private void btnSave_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string ieFG = string.Empty;
            if (rbtnImport.IsChecked.Value)
            {
                ieFG = "I";
            }
            else
            {
                ieFG = "E";
            }
            ServiceReference_HB.MainSeaRoutes mRoutes = comboxServiceLine.SelectedItem as ServiceReference_HB.MainSeaRoutes;
            int service_ID = 0;
            if (mRoutes != null)
            {
                service_ID = mRoutes.ID;
            }
            else
            {
                MessageBox.Show("请选择服务航线。"); return;
            }
            ServiceReference_HB.MainSeaRoutes mseaRoutes = comboxVesselDate.SelectedItem as ServiceReference_HB.MainSeaRoutes;
            int flt_id = 0;
            if (mseaRoutes != null)
            {
                flt_id = mseaRoutes.ID;
            }
            else
            {
                MessageBox.Show("请选择班期。"); return;
            }
            ServiceReference_HB.UM_Ref_Code mtrade = comboxINTRADE.SelectedItem as ServiceReference_HB.UM_Ref_Code;
            string trade = string.Empty;
            if (mtrade != null)
            {
                trade = mtrade.RV_LOW_VALUE;
            }

            if (Is_Add_Modefy)
            {// 新增:
                clientService.InsertUpdateProcessVoyageAsync(1, 0, int.Parse(mUser_ID), ieFG, service_ID, int.Parse(mSCD_ID), flt_id, this.txtImportVoyage.Text.Trim(), trade);
            }
            else
            {//修改:
                if (mVoyages == null) return;
                clientService.InsertUpdateProcessVoyageAsync(2, Convert.ToInt32(mVoyages.VOY_ID), int.Parse(mUser_ID), ieFG, service_ID, int.Parse(mSCD_ID), flt_id, this.txtImportVoyage.Text.Trim(), trade);
            }
            clientService.InsertUpdateProcessVoyageCompleted -= new EventHandler<ServiceReference_HB.InsertUpdateProcessVoyageCompletedEventArgs>(clientService_InsertUpdateProcessVoyageCompleted);
            clientService.InsertUpdateProcessVoyageCompleted += new EventHandler<ServiceReference_HB.InsertUpdateProcessVoyageCompletedEventArgs>(clientService_InsertUpdateProcessVoyageCompleted);
        }

        void clientService_InsertUpdateProcessVoyageCompleted(object sender, ServiceReference_HB.InsertUpdateProcessVoyageCompletedEventArgs e)
        {
            string str = e.Result;
            if (str.Substring(0, 1) == "1")
            {
                MessageBox.Show(str.Substring(2, str.Length - 2));
                OnAdd_ModefyEvent(sender, null);//通知主窗口更新数据。
                comboxSeaRoutes.SelectedItem = null;
                comboxServiceLine.SelectedItem = null;
                comboxVesselDate.SelectedItem = null;
                txtImportVoyage.Text = string.Empty;
                comboxINTRADE.SelectedItem = null;
                mVoyages = null;//清空传过来的对像
            }
            else
            {
                MessageBox.Show(str);
            }
            this.Close();
        }

        //
        private void comboxSeaRoutes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (!string.IsNullOrEmpty(mUser_ID))
                {
                    //获取登陆人的名字
                    int intID = 0;
                    int.TryParse(mUser_ID, out intID);
                    ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;

                    //绑定服务航线的下拉框
                    if (intID == 1)
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "1", item.ID);
                    else
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "0", item.ID);
                    clientService.GetChildSeaRoutes_ByLineIDCompleted -= new EventHandler<ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs>(clientService_GetChildSeaRoutes_ByLineIDCompleted);
                    clientService.GetChildSeaRoutes_ByLineIDCompleted += new EventHandler<ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs>(clientService_GetChildSeaRoutes_ByLineIDCompleted);
                    //获取班期
                }
            }
        }

        private ObservableCollection<ServiceReference_HB.MainSeaRoutes> mListServiceLine = null;
        void clientService_GetChildSeaRoutes_ByLineIDCompleted(object sender, ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs e)
        {
            mListServiceLine = e.Result;
            comboxServiceLine.ItemsSource = mListServiceLine;
            if (SCD_SLN_ID != 0)
            {
                var result = (from table in mListServiceLine
                              where table.ID == SCD_SLN_ID
                              select table).ToList();
                if (result != null && result.Count > 0)
                {
                    comboxServiceLine.SelectedItem = result[0];
                }
            }
        }

        private void comboxServiceLine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (!string.IsNullOrEmpty(mUser_ID))
                {
                    //获取登陆人的名字
                    int intID = 0;
                    int.TryParse(mUser_ID, out intID);
                    ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                    //班期
                    clientService.GetVesselDateAsync(intID, item.ID.ToString());
                    clientService.GetVesselDateCompleted -= new EventHandler<ServiceReference_HB.GetVesselDateCompletedEventArgs>(clientService_GetVesselDateCompleted);
                    clientService.GetVesselDateCompleted += new EventHandler<ServiceReference_HB.GetVesselDateCompletedEventArgs>(clientService_GetVesselDateCompleted);
                }
            }
        }

        void clientService_GetVesselDateCompleted(object sender, ServiceReference_HB.GetVesselDateCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
            comboxVesselDate.ItemsSource = col;
            if (mVoyages != null)
            {
                //班期
                List<ServiceReference_HB.MainSeaRoutes> result = (from table in col
                                                                  where table.ID == mVoyages.VOY_FLT_ID
                                                                  select table).ToList();
                if (result != null && result.Count > 0)
                {
                    comboxVesselDate.SelectedItem = result[0];
                }
                //ObservableCollection<ServiceReference_HB.MainSeaRoutes> flight = new ObservableCollection<ServiceReference_HB.MainSeaRoutes>();
                //ServiceReference_HB.MainSeaRoutes route = new ServiceReference_HB.MainSeaRoutes();
                //route.ID = int.Parse(mVoyages.VOY_FLT_ID.ToString());
                //route.RouteName = mVoyages.FLT_SCHDUL;
                //flight.Add(route);
                //comboxVesselDate.ItemsSource = flight;

            }
        }

        private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            comboxVesselDate.ItemsSource = null;
            comboxServiceLine.ItemsSource = null;

            comboxSeaRoutes.SelectedItem = null;
            comboxServiceLine.SelectedItem = null;
            comboxVesselDate.SelectedItem = null;
            txtImportVoyage.Text = string.Empty;
            comboxINTRADE.SelectedItem = null;

            rbtnImport.IsChecked = true;
            rbtnExport.IsChecked = false;
            txtCountTen.Text = string.Empty;
            txtCountTen_Plan.Text = string.Empty;
            mVoyages = null;
            progressbar.IsBusy = false;
        }



    }//end class
}

